﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace robotics_ex_1_3
{
    class Polygon
    {
        private List<Point> _vertices;
        private List<Line> _edges;
        private Point _refPoint;

        public Polygon(UInt32 numOfVertices, string vertices)
        {
            _vertices = new List<Point>();

            string[] values = vertices.Split(' ');

            for (UInt32 i = 0; i < numOfVertices; ++i)
            {
                Int32 currentX = System.Convert.ToInt32(values[i]);
                Int32 currentY = System.Convert.ToInt32(values[i + 1]);

                Point currentPoint = new Point(currentX, currentY);

                _vertices.Add(currentPoint);
            }

            initVectors();

            _refPoint = _vertices[0];
        }

        private void initVectors()
        {
            _edges = new List<Line>();

            for (Int32 i = 0; i < Vertices.Count - 1; ++i)
            {
                _edges.Add(new Line(Vertices[i], Vertices[i + 1]));
            }

            _edges.Add(new Line(Vertices[Vertices.Count - 1], Vertices[0]));

            //self.__normal_diagram.sort( key = lambda normal: normal.angle )  
        }

        public Point getFirstMeetingPoint(Line edge)
        {
            if (edge.Angle < _edges[0].Angle)
            {
                return _edges[0].StartPoint;
            }

            foreach (Line currentRobotEdge in _edges)
            {
                if (edge.Angle >= currentRobotEdge.Angle)
                {
                    return currentRobotEdge.EndPoint;
                }
            }

            throw new Exception("No suitable point touch the edge " + edge.ToString());
        }

        public List<Point> Vertices
        {
            get { return _vertices; }
            set { _vertices = value; }
        }

        public List<Line> Edges
        {
            get { return _edges; }
            set { _edges = value; }
        }

        public robotics_ex_1_3.Point RefPoint
        {
            get { return _refPoint; }
            set { _refPoint = value; }
        }
    }

    class Point
    {
        Int32 x;
        Int32 y;

        public Point(Int32 x_i, Int32 y_i)
        {
            x = x_i;
            y = y_i;
        }

        public override string ToString()
        {
            return string.Format("({0},{1}) -> ({2},{3})");
        }

        public System.Int32 X
        {
            get { return x; }
            set { x = value; }
        }

        public System.Int32 Y
        {
            get { return y; }
            set { y = value; }
        }
    }

}
